﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：逻辑层辅助类；
  //创建人：赵国伟
  //生成时间：2012/04/02 14:37:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Shopping.Model;
using Shopping.Common;
using Shopping.DAL;

namespace Shopping.BLL
{

    public class BLLHelper
    {

        #region "将指定的IDNameObj数据生成为下拉框选项； by 赵国伟 2012-04-02"
        /// <summary>
        /// 将指定的IDNameObj数据生成为下拉框选项；
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GenerateToSelectOption(List<IDNameObj> datas, string defaultValue)
        {
            StringBuilder sb = new StringBuilder(500);
            if (datas != null)
            {
                foreach (IDNameObj datainfo in datas)
                {
                    sb.Append("<option value=\"");
                    sb.Append(datainfo.ID);
                    sb.Append("\" ");
                    if (defaultValue == datainfo.ID.ToString())
                        sb.Append(" Selected ");
                    sb.Append(">");
                    sb.Append(datainfo.Name);
                    sb.Append("</option>");
                }
            }
            return sb.ToString();
        }
        #endregion

        #region "根据查询条件查询指定表的字段信息并输出为下拉选项；by 赵国伟 2012-04-22"
        /// <summary>
        /// 根据查询条件查询指定表的字段信息并输出为下拉选项；
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cond"></param>
        /// <param name="objEnum"></param>
        /// <returns></returns>
        public static string GenerateToSelectOption<T>(string cond, Enum objEnum, string defaultValue)
             where T : class  //限制该泛型必须为引用类型；
        {
            return GenerateToSelectOption(DALHelper.QueryIDNameObjs<T>(cond, objEnum),defaultValue);
        }
        #endregion

        #region "将指定的IDNameObj数据生成到Hash结构中 by 赵国伟 2012-04-02"
        /// <summary>
        /// 将指定的IDNameObj数据生成到Hash结构中
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static Dictionary<int,string> GenerateToHash(List<IDNameObj> datas)
        {
            Dictionary<int, string> dict = new Dictionary<int, string>();
            if (datas != null)
            {
                foreach (IDNameObj data in datas)
                    dict.Add(data.ID, data.Name);
            }
            return dict;
        }
        #endregion

        #region "根据查询条件查询指定表的字段信息并输出为Hash结构 by 赵国伟 2012-04-14"
        /// <summary>
        /// 根据查询条件查询指定表的字段信息并输出为Hash结构
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cond"></param>
        /// <param name="objEnum"></param>
        /// <returns></returns>
        public static Dictionary<int, string> GenerateToHash<T>(string cond, Enum objEnum)
             where T : class  //限制该泛型必须为引用类型；
        {
            return GenerateToHash(DALHelper.QueryIDNameObjs<T>(cond, objEnum));
        }
        #endregion


        #region "将指定的IDNameObj数据生成为复选框选项； by 赵国伟 2012-04-07"
        /// <summary>
        /// 将指定的IDNameObj数据生成为复选框选项；
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="chkObjectName"></param>
        /// <param name="defaultValue"></param>
        /// <param name="maxLineNumber"></param>
        /// <returns></returns>
        public static string GenerateToCheckOption(List<IDNameObj> datas, string chkObjectName, string defaultValue, int maxLineNumber)
        {
            StringBuilder sb = new StringBuilder(500);
            if (datas != null)
            {
                defaultValue = "," + defaultValue + ",";
                int i = 1;
                foreach (IDNameObj datainfo in datas)
                {
                    sb.Append("<label for=\"" + chkObjectName + "Of" + datainfo.ID + "\">");
                    sb.Append("<input name=\"");
                    sb.Append(chkObjectName);
                    sb.Append("\" type=\"checkbox\" value=\"");
                    sb.Append(datainfo.ID);
                    sb.Append("\"  id=\"" + chkObjectName + "Of" + datainfo.ID + "\" ");
                    if (defaultValue.IndexOf("," + datainfo.ID + ",") >= 0)
                        sb.Append(" checked ");
                    sb.Append("/> ");
                    sb.Append(datainfo.Name);
                    sb.Append("</label>");
                    sb.Append("&nbsp;");
                    if (maxLineNumber > 0)
                    {
                        if (i % maxLineNumber == 0)
                        {
                            sb.Append(" <br>");
                        }
                    }
                    i++;
                }
            }
            return sb.ToString();
        }
       
        #endregion

        #region "执行指定业务对象上的保存方法； by 赵国伟 2012-04-02"
        /// <summary>
       /// 执行指定业务对象上的保存方法；
       /// </summary>
       /// <typeparam name="T1"></typeparam>
       /// <typeparam name="T2"></typeparam>
       /// <param name="methodName"></param>
       /// <returns></returns>
        public static ReturnValue Save<T1, T2>(string methodName) 
            where T2 : class  //限制该泛型必须为引用类型；
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败！");
            string jsonData = Utility.P("jsonData");  //获取传递过来的Json对象数据；
            T2 info = default(T2);
            try
            {
                ///将提交上来的Json格式的数据装化为相应的实体类；
                info = JSONHelper.JsonStringToObject<T2> (jsonData);
            }
            catch (Exception e)
            {
                ret.Message = "数据提取失败！" + e.Message;
                return ret;
            }
            if (info != null)
            {
                Type classType = typeof(T1); //获得类型信息；
                Assembly app = Assembly.GetAssembly(classType); //加载应用程序集集
                object bllObj = app.CreateInstance(classType.FullName); //创建该业务对象的示例对象；
                MethodInfo method = classType.GetMethod(methodName); //查找执行反射的方法；
                ret = (ReturnValue)method.Invoke(bllObj, new object[] { info }); //执行反射方法；
            }
            else
                ret.Message = "提交数据不能为空！";
            return ret;
        }
        #endregion



    }
}
